//
//  YTOrderDetailViewController+Navigation.m
//  YiTuDriverClient
//
//  Created by 石少庸 on 2017/7/26.
//  Copyright © 2017年 Yitu. All rights reserved.
//

#import "YTOrderDetailViewController+Navigation.h"

@implementation YTOrderDetailViewController (Navigation)
- (void)navigationWithCoordinate:(CLLocationCoordinate2D)coordinate
                    locationName:(NSString *)locationName {
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"选择地图" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    [alertController addAction:[UIAlertAction actionWithTitle:@"苹果地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //使用自带地图导航
        MKMapItem *currentLocation =[MKMapItem mapItemForCurrentLocation];
        
        MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil]];
        toLocation.name = locationName;
        [MKMapItem openMapsWithItems:@[currentLocation,toLocation] launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
                                                                                   MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];
    }]];
    
    //判断是否安装了高德地图，如果安装了高德地图，则使用高德地图导航
    if ( [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *urlsting =[[NSString stringWithFormat:@"iosamap://navi?sourceApplication= &backScheme= &lat=%f&lon=%f&dev=0&style=2",coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [[UIApplication  sharedApplication]openURL:[NSURL URLWithString:urlsting]];
        }]];
    }
    
    //判断是否安装了百度地图，如果安装了百度地图，则使用百度地图导航
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
        [alertController addAction:[UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *urlsting =[[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=目的地&mode=driving&coord_type=gcj02",coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlsting]];
        }]];
    }
    
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
        [alertController addAction:[UIAlertAction actionWithTitle:@"谷歌地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",@"易途8", @"QQ41e6e163",coordinate.latitude, coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
        }]];
    }
    
    //添加取消选项
    [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [alertController dismissViewControllerAnimated:YES completion:nil];
    }]];
    
    [self.navigationController presentViewController:alertController animated:YES completion:nil];
}


/**
 创建左右各一个label
 
 @param leftString 左边title
 @param rightString 右边titile
 @param superView 父view
 @param index index
 */
- (void)creatLeftAndRightLabel:(NSString *)leftString right:(NSString *)rightString
                     superView:(UIView *)superView index:(NSInteger)index {
    UILabel *left = [[UILabel alloc] init];
    left.font = [UIFont systemFontOfSize:16];
    left.textColor = [UIColor colorWithRed:0.44 green:0.44 blue:0.44 alpha:1.00];
    left.text = leftString;
    [superView addSubview:left];
    [left mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(superView.mas_left).offset(15);
        make.top.equalTo(superView.mas_top).offset(index * 28 + 10);
    }];
    
    UILabel *right = [[UILabel alloc] init];
    [superView addSubview:right];
    right.textColor = [UIColor colorWithRed:0.26 green:0.26 blue:0.26 alpha:1.00];
    right.font = [UIFont systemFontOfSize:14];
    right.text = rightString;
    [right mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(left);
        make.right.equalTo(superView.mas_right).offset(-15);
        make.left.equalTo(left.mas_right).offset(15);
    }];
}

@end
